//
//  BLCommentViewController.m
//  百思不得姐
//
//  Created by hbl on 16/3/29.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLCommentViewController.h"
#import "BLTopicTableViewCell.h"
#import "BLTopic.h"
#import "BLNetwork.h"
#import "BLUser.h"
#import "MJExtension.h"
#import "BLCommentTableViewCell.h"
#import "BLComment.h"
#import "MJRefresh.h"

@interface BLCommentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewMargin;
@property (nonatomic, strong)NSArray *hotComment;
@property (nonatomic, strong)NSMutableArray *allComment;
@property (nonatomic, assign)NSInteger total;
@end

@implementation BLCommentViewController

- (NSMutableArray *)allComment
{
    if (!_allComment) {
        _allComment = [NSMutableArray array];
    }
    return _allComment;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"评论";
    self.view.backgroundColor = BLBg;
    self.tableView.backgroundColor = BLBg;
    [self.tableView registerNib:[UINib nibWithNibName:@"BLCommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"comment"];
   
    [self setupHead];
    [self setupKeyBoard];
    [self.tableView.mj_header beginRefreshing];
}

- (void)getData
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    params[@"a"] = @"dataList";
    params[@"c"] = @"comment";
    params[@"data_id"] = self.topic.id;
    params[@"hot"] = @"1";

   [BLNetwork POST:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
       NSArray *temp = result;
      
       if (temp.count != 0) {
           self.total = [result[@"total"] intValue];
           self.hotComment = [BLComment mj_objectArrayWithKeyValuesArray:result[@"hot"]];
           self.allComment = [BLComment mj_objectArrayWithKeyValuesArray:result[@"data"]];
           [self.tableView reloadData];
       }
       
       [self.tableView.mj_header endRefreshing];
   } failure:^(NSError *error) {
       [self.tableView.mj_header endRefreshing];
   }];
}

- (void)loadMore
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    params[@"a"] = @"dataList";
    params[@"c"] = @"comment";
    params[@"data_id"] = self.topic.id;
    params[@"hot"] = @"1";
    BLComment *lastComment = [self.allComment lastObject];
    params[@"lastcid"] = lastComment.id;
    [BLNetwork POST:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
        NSArray *resultArray = result;
        if (resultArray.count != 0) {
            NSArray *temp = [BLComment mj_objectArrayWithKeyValuesArray:result[@"data"]];
            [self.allComment addObjectsFromArray:temp];
            [self.tableView reloadData];
            if (self.total == self.allComment.count) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
      
        
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)setupKeyBoard
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(chaneKeyBoard:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)chaneKeyBoard:(NSNotification *)note
{
    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.bottomViewMargin.constant = mainH - rect.origin.y;
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
        [self.view layoutIfNeeded];
    }];
    
}


- (void)setupHead
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    UIView *head = [[UIView alloc]init];
    BLTopicTableViewCell *cell = [BLTopicTableViewCell cell];
    cell.topic = self.topic;
    cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, mainW, self.topic.cellHight);
    head.frame = CGRectMake(head.frame.origin.x, head.frame.origin.y, mainW, self.topic.cellHight);
    [head addSubview:cell];
     self.tableView.tableHeaderView = head;
   }

- (void)dealloc{
}

#pragma delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.allComment.count == 0) {
        return 0;
    }
    if (self.hotComment.count == 0) {
        return 1;
    }
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.hotComment.count == 0) {
            return self.allComment.count;
        }
        return self.hotComment.count;
    }else{
        return self.allComment.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *tempArray = [NSArray array];
    BLCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment"];
   
    if (self.hotComment.count == 0) {
        tempArray = self.allComment;
    }else{
        if (indexPath.section == 0) {
            tempArray = self.hotComment;
        }else{
            tempArray = self.allComment;
        }
    }
    BLComment *comment = tempArray[indexPath.row];
    cell.comment = comment;
    return cell;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BLComment *comment;
    if (indexPath.section == 0) {
        if (self.hotComment.count == 0) {
           comment = self.allComment[indexPath.row];
        }else{
        comment = self.hotComment[indexPath.row];
        }
        
    }else{
        comment = self.allComment[indexPath.row];
    }
    return comment.cellHight;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.hotComment.count == 0) {
         return @"最新评论";
        }
        return @"最热评论";
    }else{
        return @"最新评论";
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *head = [[UIView alloc]init];
    head.backgroundColor = BLBg;
    UILabel *title = [[UILabel alloc]init];
    title.frame = CGRectMake(7, 0, 300, 30);
    [title setTextColor:[UIColor darkGrayColor]];
    if (section == 0) {
        if (self.hotComment.count == 0) {
            title.text = @"最新评论";
        }else{
        title.text = @"最热评论";
        }
       
    }else{
        title.text = @"最新评论";
    }

    [head addSubview:title];
    return head;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
